import { createRouter, createWebHistory } from 'vue-router'
// import useDataStore from '../store'
// import { storeToRefs } from 'pinia';


// const dataStore = useDataStore()
// const { userInfo, isLogin } = storeToRefs(dataStore)


// 创建路由表
const routes = [
    {
        path: '/',
        redirect: '/home',
        component: () => import('@/views/layout/index.vue'),
        children: [
            {
                path: 'home',
                name: 'home',
                component: () => import('@/views/home/index.vue')
            },
            // {
            //     path: 'detail',
            //     name: 'detail',
            //     component: () => import('@/components/detail.vue')
            // },
            {
                path: 'article',
                name: 'article',
                component: () => import('@/views/article/index.vue')
            },
            {
                path: 'publish',
                name: 'publish',
                component: () => import('@/views/publish/index.vue')
            },
            {
                path: 'user',
                name: 'user',
                component: () => import('@/views/user/index.vue'),
                // beforeEnter: (to, from, next) => {
                //     if (from.name === 'me') {
                //         console.log('fromme');

                //         location.reload()
                //         next()
                //     } else {
                //         next()
                //     }
                // }
            },
            {
                path: 'rate',
                name: 'rate',
                component: () => import('@/views/rate/index.vue')
            },
            {
                path: 'me',
                name: 'me',
                component: () => import('@/views/me/index.vue')
            },
            {
                path: 'test',
                name: 'test',
                component: () => import('@/views/test/index.vue')
            },
            {
                path: 'mobileSearch',
                name: 'mobileSearch',
                component: () => import('@/views/mobileSearch/index.vue')
            },
            {
                path: 'shop',
                name: 'shop',
                // component: () => import('@/views/shop/index.vue')
                component: () => import('@/test/deepseek.vue')
            },
            {
                path: 'shopDetail',
                name: 'shopDetail',
                component: () => import('@/views/shop/components/shopDetail.vue')
            },
            {
                path: 'messageBoard',
                name: 'messageBoard',
                component: () => import('@/views/messageBoard/index.vue')
            },
        ]

    },
    {
        path: '/login',
        name: 'login',
        component: () => import('@/views/login/index.vue')
    },
    {
        path: '/register',
        name: 'register',
        component: () => import('@/views/register/index.vue')
    },
    // {
    //     path: '/containerList',
    //     name: 'containerList',
    //     component: () => import('@/test/ContainerList.vue')
    // },
    {
        path: '/:catchAll(.*)',
        name: 'notFound',
        component: () => import('@/views/notFound/index.vue'),
    },

]

// 创建路由对象
const router = createRouter({
    history: createWebHistory(),
    routes
})

router.beforeEach((to, from, next) => {
    const isLogin = localStorage.getItem('isLogin')
    if (isLogin || to.name === 'home' || to.name === 'login' || to.name === 'register') {
        next()
    } else {
        ElMessage({
            type: 'warning',
            message: '请先登录'
        })
        next({ name: 'login' })
    }



})

//向外导出路由对象
export default router